﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sealight.Core.Domain;
using Sealight.API.Framework;
using NHibernate;
using NHibernate.Criterion;

namespace Sealight.Data
{
   public class ContentDataRepository: NHibernateRepository<ContentEntity>, IContentDataRepository
    {
        #region IContentDataRepository Members

        public IList<ContentCategoryEntity> GetAllContentCategory()
        {
            IList<ContentCategoryEntity> results = null;
            try
            {
                results = UnitOfWork.CurrentSession.CreateCriteria(typeof(ContentCategoryEntity))
                    .List<ContentCategoryEntity>();
            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }

            return results;
        }

        public ContentCategoryEntity GetContentCategory(Guid id)
        {
            ContentCategoryEntity results = null;
            try
            {
                results = UnitOfWork.CurrentSession.Get<ContentCategoryEntity>(id);
            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }
            return results;
        }

        public IList<ContentEntity> GetAllContentByCategory(Guid id)
        {
            IList<ContentEntity> results = null;
            try
            {
                results = UnitOfWork.CurrentSession.CreateCriteria(typeof(ContentEntity))                    
                    .Add(Expression.Eq("ContentCategory.ContentCategoryID", id))
                    .List<ContentEntity>();
            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }

            return results;
        }

        #endregion
    }
}
